<?php

namespace app\circle\controller;

use app\circle\model\CircleMemberModel;
use app\circle\model\MeetingCompanyModel;
use app\circle\model\MeetingContentModel;
use app\circle\model\MeetingModel;
use app\circle\model\MeetingSignModel;
use app\circle\model\MemberModel;
use think\Request;

class MeetingController extends BaseController
{
    private $meetingModel;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->meetingModel = new MeetingModel();
    }

    /**
     * 会议列表
     * 具体列表以ajax的形式获取
     * @return mixed|\think\Paginator
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $meetingState = $this->request->param('meeting_state', 0);//0正在进行 1 即将开始 2 往期回顾
            $map = [];
            switch ($meetingState) {
                case 0:
                    $map['start_time'] = ['<', time()];
                    $map['end_time'] = ['>', time()];
                    break;
                case 1:
                    $map['start_time'] = ['>', time()];
                    break;
                case 2:
                    $map['end_time'] = ['<', time()];
                    break;
            }
            $list = $this->meetingModel->where($map)->paginate(10, false, ['query' => $this->request->request()]);
            return json($list);
        }
        return $this->fetch();
    }

    public function info()
    {
        // 创建SDK实例
        $script = &load_wechat('Script', $this->getWxconfig());
        // 获取JsApi使用签名，通常这里只需要传 $ur l参数
        $options = $script->getJsSign(get_current_url());
        $this->assign('options', $options);
        $meetingId = $this->request->param('meeting_id', 0);
        $meetingInfo = $this->meetingModel->where('meeting_id', $meetingId)->find();
        $isJoin = 0;
        $memberId = $this->getMemberId();
        if ($memberId) {
            $model = new CircleMemberModel();
            $isJoin = $model->where(['circle_id' => $meetingInfo['circle_id'], 'member_id' => $memberId])->count();
        }
        $this->assign('isJoin', $isJoin);
        $this->assign('meetingInfo', $meetingInfo);
        return $this->fetch();
    }

    /**
     * 会议报名
     */
    public function sign()
    {
        $data = $this->request->param();
        $smsValid = session('sms_valid');
        if ($smsValid[1] < time() - 3 * 60) {
            session('sms_valid', null);
            $this->error('短信验证码已失效!');
        } else if ($smsValid[0] != $data['mcode']) {
            $this->error('短信验证码错误');
        }
        $result = $this->validate($data, [
            'meeting_id|会议' => 'require',
            'mobile|手机号' => 'number|length:11',
            'name|姓名' => 'require',
        ]);
        if (true !== $result) {
            $this->error($result);
        }
        $meetingSignModel = new MeetingSignModel();
        if ($meetingSignModel->sign($data['meeting_id'], $data['mobile'], $data['name'])) {
            $this->success('报名成功');
        } else {
            $this->error($meetingSignModel->getError());
        }
    }

    /**
     * 绑定公司(其实就是注册会员)
     */
    public function bindCompany()
    {
        $data = $this->request->param();
        $result = $this->validate($data, [
            'meeting_id' => 'require',
            'company_code|企业认证码' => 'require',
            'member_mobile|手机号' => 'number|length:11',
            'member_duties|职务' => 'require',
            'member_truename|姓名' => 'require',
            ['meeting_id', 'require', '会议未指定']

        ]);
        if (true !== $result) {
            $this->error($result);
        }

        $memberModel = new MemberModel();
        $memberId = $this->getMemberId();
        if ($memberModel->bindCompany($data, $memberId)) {
            $this->success('绑定成功');
        } else {
            $this->error($memberModel->getError());
        }
    }

    /**
     * 会议资讯
     * @return mixed|\think\Paginator
     */
    public function meetingContent()
    {
        $meetingId = $this->request->param('meeting_id', 0);
        $model = new MeetingContentModel();
        $list = $model->where('meeting_id', $meetingId)->paginate(10);
        foreach ($list as $index => $value) {
            $list[$index]['create_time'] = format_time($str = 'm-d', $value['create_time']);
            $list[$index]['desc'] = mb_substr(trim(strip_tags($value['content'])),0,100,'utf-8');
        }
        if ($this->request->isAjax()) {
            return $list;
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function meetingContentInfo()
    {
        $id = $this->request->param('id', 0);
        $model = new MeetingContentModel();
        $info = $model->where('id', $id)->find();
        $this->assign('meetingInfo', $info);
        return $this->fetch();
    }

    /**
     * 签到
     */
    public function meetingSign()
    {
        $meetingId=$this->request->param('meeting_id',0);
        if (empty($meetingId)){
            $this->error('会议不明确');
        }
        $meetingCompanyModel=new MeetingCompanyModel();
        $meetingInfo=$this->meetingModel->where('meeting_id',$meetingId)->find();
        $this->assign('meetingInfo',$meetingInfo);
        $memberId=$this->getMemberId();
        $this->assign('memberId',$memberId);
        $isJoin=0;//0 不在邀请范围内 1 需要登陆 2 可以签到 3 已经签到
        if ($memberId){
            $memberModel=new MemberModel();
            $memberInfo=$memberModel->where(['member_id'=>$memberId])->find();
            $isJoin=$meetingCompanyModel->where(['company_id'=>$memberInfo['company_id'],'meeting_id'=>$meetingId])->count();
            if ($isJoin){
                $isJoin=2;
            }
            $circleMemberModel=new CircleMemberModel();
            $circleMemberCount=$circleMemberModel->where(['member_id'=>$memberId,'circle_id'=>$meetingInfo['circle_id']])->count();
            if ($circleMemberCount){
                $isJoin=3;
            }
        }else{
            $isJoin=1;
        }
        $this->assign('is_join',$isJoin);
        if ($this->request->isPost()){
            $data = $this->request->param();
            $memberModel = new MemberModel();
            $memberId = $this->getMemberId();
            $memberInfo=$memberModel->where(['member_id'=>$memberId])->find();
            $data['member_mobile']=$memberInfo['member_mobile'];
            $data['member_duties']=$memberInfo['member_duties'];
            $data['member_truename']=$memberInfo['member_truename'];
            $result = $this->validate($data, [
                'meeting_id' => 'require',
                'company_code|企业认证码' => 'require',
                'member_mobile|手机号' => 'number|length:11',
                'member_duties|职务' => 'require',
                'member_truename|姓名' => 'require',

            ]);
            if (true !== $result) {
                $this->error($result);
            }

            if ($memberModel->bindCompany($data, $memberId)) {
                $this->success('绑定成功');
            } else {
                $this->error($memberModel->getError());
            }
        }
        return $this->fetch();
    }

}